
#include"../../../speech/aip-cpp-sdk-4.16.7/speech.h"
#include"../../common/etcd.hpp"
#include<gflags/gflags.h>
#include"base.pb.h"
#include"user.pb.h"
#include"transmite.pb.h"
#include"../../common/channel.hpp"
#include"utils.hpp"
#include<gtest/gtest.h>
#include<thread>

DEFINE_bool(run_mode,false,"程序运行的模式,false-调试,true-发布");
DEFINE_string(log_file,"","发布模式下,用于指定日志输出的文件");
DEFINE_int32(log_level,0,"发布模式下,用于指定日志输出等级");

DEFINE_string(etcd_host,"http://127.0.0.1:2379","服务注册中心地址");
DEFINE_string(base_service,"/service","服务监控根目录");
DEFINE_string(transmite_service,"/service/transmite_service","服务监控根目录");

ServiceManager::ptr sm;

void string_message(const std::string& uid,const std::string& sid,const std::string& msg)
{

    //3.通过RPC信道管理对象,获取提供Echo服务的信道
    auto channel = sm->choose(FLAGS_transmite_service);
    if(!channel)
    {
        LOG_ERROR("获取通信信道失败");
        return ;
    }
    wzl::MsgTransmitService_Stub stub(channel.get());
    wzl::NewMessageReq req;
    req.set_request_id(wzl::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(wzl::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content(msg);

    wzl::GetTransmitTargetRsp* resp = new wzl::GetTransmitTargetRsp();
    brpc::Controller* cntl = new brpc::Controller();
    stub.GetTransmitTarget(cntl,&req,resp,nullptr);
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(resp->success());
}



void image_message(const std::string& uid,const std::string& sid,const std::string& msg)
{

    //3.通过RPC信道管理对象,获取提供Echo服务的信道
    auto channel = sm->choose(FLAGS_transmite_service);
    if(!channel)
    {
        LOG_ERROR("获取通信信道失败");
        return ;
    }
    wzl::MsgTransmitService_Stub stub(channel.get());
    wzl::NewMessageReq req;
    req.set_request_id(wzl::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(wzl::MessageType::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_content(msg);

    wzl::GetTransmitTargetRsp* resp = new wzl::GetTransmitTargetRsp();
    brpc::Controller* cntl = new brpc::Controller();
    stub.GetTransmitTarget(cntl,&req,resp,nullptr);
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(resp->success());
}

void file_message(const std::string& uid,const std::string& sid,const std::string& filename,const std::string& content)
{
        //3.通过RPC信道管理对象,获取提供Echo服务的信道
    auto channel = sm->choose(FLAGS_transmite_service);
    if(!channel)
    {
        LOG_ERROR("获取通信信道失败");
        return ;
    }
    wzl::MsgTransmitService_Stub stub(channel.get());
    wzl::NewMessageReq req;
    req.set_request_id(wzl::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(wzl::MessageType::FILE);
    req.mutable_message()->mutable_file_message()->set_file_contents(content);
    req.mutable_message()->mutable_file_message()->set_file_name(filename);
    req.mutable_message()->mutable_file_message()->set_file_size(content.size());

    wzl::GetTransmitTargetRsp* resp = new wzl::GetTransmitTargetRsp();
    brpc::Controller* cntl = new brpc::Controller();
    stub.GetTransmitTarget(cntl,&req,resp,nullptr);
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(resp->success());
}


void speech_message(const std::string &uid, const std::string &sid, const std::string &msg) {
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel) {
        LOG_ERROR("获取通信信道失败");
        return;
    }
    wzl::MsgTransmitService_Stub stub(channel.get());
    wzl::NewMessageReq req;
    wzl::GetTransmitTargetRsp rsp;
    req.set_request_id(wzl::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(wzl::MessageType::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_file_contents(msg);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}



int main(int argc,char* argv[])
{
    google::ParseCommandLineFlags(&argc,&argv,true);
    wzl::init_logger(FLAGS_run_mode,FLAGS_log_file,FLAGS_log_level);

    //1.先构造PRC信道管理对象
    sm = std::make_shared<ServiceManager>();
    sm->declared(FLAGS_transmite_service);
    auto put_cb = std::bind(&ServiceManager::onServiceOnline,sm.get()\
    ,std::placeholders::_1,std::placeholders::_2);
    auto del_cb = std::bind(&ServiceManager::onServiceOffline,sm.get()\
    ,std::placeholders::_1,std::placeholders::_2);
    //2.构造服务发现对象
    Discovery::ptr dclient = std::make_shared<Discovery> (FLAGS_etcd_host,FLAGS_base_service,put_cb,del_cb);
     string_message("d303-acc6df72-0001","会话ID1","吃饭了吗");
    // string_message("af0a-97ab10fa-0000","会话ID1","吃的盖浇饭");

    // image_message("ea23-7fd90c52-0000","会话ID1","萌萌表情图片");

    // speech_message("ea23-7fd90c52-0000","会话ID1","动听猪叫声数据");

    file_message("d303-acc6df72-0001","4d05-82a88ed1-0000","猪爸爸的文件名称","猪爸爸的文件数据");

    return 0;
}
